import QtQuick 2.12

import QtQuick.Controls 1.4
import QtQuick.Controls 2.7

import QtQuick.Layouts 1.12


Page {
    id: page1
    visible: false
    property alias tabBtnLayout: navlyout
    header:Rectangle {
        height: 64
        color: "green"
        RowLayout {
            anchors.fill: parent
            Button {
                id: retbtn
                width: 60
                height: 48
                background: Item {
                    opacity: 0
                }
                onClicked:  {
                    stackView.pop()
                }
                icon {
                    source: "qrc:/images/icon_back.png"
                    color: "white"
                    height: 36
                    width: 36
                }
            }

            Text {
                text: qsTr("日志查看")
                font.pixelSize: 22
            }
            Item {
                Layout.fillWidth: true
            }
            Text {
                id: time2;
                color: "#fff";
                font.pointSize: 22;
                Layout.alignment: Qt.AlignRight
            }

            Timer {
                interval: 1000;
                running: true;
                repeat: true;
                onTriggered: time2.text =
                             Qt.formatDateTime(new Date(), "hh:mm:ss");

            }
        }
    }
    StackLayout {
        id: stackLayout
        anchors.left: rootitem.right
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        currentIndex: 0
        Component {
            id: ccalendarPopupComp
            Item {}
                    }
        Component {
            id: cellDelegate
            Rectangle {
                color: "transparent"
                Text {
                    text: styleData.value
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
        Component {
            id: timeCellDelegate
            Rectangle {
                color: "transparent"
                Text {
                    text: String(styleData.value)
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
        }
        Component {
            id: row_delegate
            Rectangle {
                height: 30
                color: {
                    if(styleData.pressed)
                        return "#66b2ff"
                    else if(styleData.row & 0x1)
                        return "#fff"
                    else
                        return  "#eee"
                }
            }
        }
        Component {
            id: headerDlg
            Rectangle {
                height: 30
                color: "#eee"
                border.width: 1
                border.color: "#ccc"
                Text {
                    text: qsTr(styleData.value)
                    font.pixelSize: 16
                    anchors {
                        left: parent.left
                        verticalCenter: parent.verticalCenter
                        leftMargin: 5
                    }
                }

            }
        }
        ColumnLayout{
            id: auth_column
            //spacing: 5
            Rectangle {
                height: 64
                border.width: 1
                border.color: "green"
                Layout.fillWidth: true;
                Layout.margins: 5
                RowLayout {
                    id: headerRow
                    height: parent.height
                    CComboBox {
                        Layout.preferredHeight: 32
                        Layout.preferredWidth: 200
                        Layout.leftMargin: 10
                        id: libComBox
                        Component.onCompleted: {
                            var mlist = Services.getLibRoomNames()
                            mlist.splice(0, 0, qsTr("选择库室名称"))
                            model = mlist;
                        }


                    }
                    Item {
                        Layout.preferredWidth: 300

                    }
                    CCalendarBtn {
                        id: dateSlecteBtn
                        def: qsTr("选择授权日期")
                        width: 144
                        height: 32
                        //Layout.preferredHeight: 32

                    }
                    Button {
                        Layout.preferredHeight: 32
                        Layout.preferredWidth: 64
                        contentItem:
                            Text {
                            text:  qsTr("查询")
                            font.pixelSize: 16
                            verticalAlignment:  Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
                        }
                        background: Rectangle {
                            color: "green"
                        }
                        onClicked: {
                            var selectedDate = dateSlecteBtn.text===qsTr("选择授权日期")
                                    ?'%':dateSlecteBtn.text
                            var libname =  libComBox.currentText === qsTr("选择库室名称")
                                    ?'%': libComBox.currentText
                            authlog.model = Services.getAuthlogModel(
                                        libname,
                                        selectedDate);
                        }
                    }
                }
            }
            TableView {
                id: authlog;
                headerDelegate: headerDlg
                Layout.fillWidth: true;
                Layout.fillHeight: true;
                Layout.margins: 5
                rowDelegate: row_delegate
                clip: true
                TableViewColumn {
                    role: "libname"
                    title: qsTr("库室名称");

                    delegate: cellDelegate
                }
                TableViewColumn {
                    role: "req_man"
                    title: qsTr("申请人");
                    delegate: cellDelegate


                }
                TableViewColumn {
                    role: "reason"
                    title: qsTr("理由");
                    delegate: cellDelegate

                }
                TableViewColumn {
                    role: "result"
                    title: qsTr("结果");
                    delegate: Rectangle {
                        color: "transparent"
                            Text {
                            text: styleData.value===1 ?qsTr("成功"): qsTr("失败")
                            anchors.verticalCenter: parent.verticalCenter

                        }
                    }
                }
                TableViewColumn {
                    role: "auth_man"
                    title: qsTr("授权人")
                    delegate: cellDelegate

                }
                TableViewColumn {
                    role: "auth_time"
                    title: qsTr("授权时间");
                    width: 300;
                    delegate: timeCellDelegate
                }
            }

            Component.onCompleted: {
                authlog.model = Services.getAuthlogModel("%", "%");

            }
        }
        ColumnLayout{
            id: talk_column
            Rectangle {
                height: 64
                border.width: 1
                border.color: "green"
                Layout.fillWidth: true;
                Layout.margins: 5
            RowLayout {
                id: talkHeaderRow
                height: parent.height;

                CComboBox {
                    Layout.preferredHeight: 30
                    Layout.preferredWidth: 200
                    Layout.leftMargin: 10
                    id: talkLibComBox
                    Component.onCompleted: {
                        var mlist = Services.getLibRoomNames()
                        mlist.splice(0, 0, qsTr("选择库室名称"))
                        talkLibComBox.model = mlist;
                    }


                }
                Item {
                    Layout.preferredWidth: 300

                }
                CCalendarBtn {
                    id: talkDateSlected
                    def: qsTr("选择通话日期")
                    width: 144
                    height: 32
                    //Layout.preferredHeight: 32

                }
                  Button {
                      implicitWidth: 64
                      implicitHeight: 32
                      contentItem:
                          Text {
                          text:  qsTr("查询")
                          font.pixelSize: 16
                          verticalAlignment:  Text.AlignVCenter
                          horizontalAlignment: Text.AlignHCenter


                      }
                      background: Rectangle {
                          color: "green"
                      }
                    onClicked: {
                        var selectedDate = talkDateSlected.text ===qsTr("选择通话日期")
                                ?'%': talkDateSlected.text
                        var libname =  talkLibComBox.currentText === qsTr("选择库室名称")
                                ?'%': talkLibComBox.currentText
                        talklog.model = Services.getTalklogModel(
                                    libname,
                                    selectedDate);
                    }
                }
            }
}
            TableView {
                id: talklog;
                Layout.fillWidth: true;
                Layout.fillHeight: true;
                Layout.margins: 5
                rowDelegate: row_delegate
                headerDelegate: headerDlg
                TableViewColumn {
                    role: "libname"
                    title: qsTr("库室名称");
                     delegate: cellDelegate
                }
                TableViewColumn {
                    role: "talk_type"
                    title: qsTr("通话类型");
                     delegate: cellDelegate
                }
                TableViewColumn {
                    role: "talk_res"
                    title: qsTr("通话结果");
                     delegate: cellDelegate

                }
                TableViewColumn {
                    role: "req_man"
                    title: qsTr("申请人");
                     delegate: cellDelegate


                }
                TableViewColumn {
                    role: "auth_man"
                    title: qsTr("授权人")
                    delegate: cellDelegate
                }
                TableViewColumn {
                    role: "talk_time"
                    title: qsTr("通话时间");
                    delegate: timeCellDelegate
                    width: 300;
                }
            }
            Component.onCompleted: {
                talklog.model = Services.getTalklogModel("%", "%");

            }

        }

        ColumnLayout{
            id: broadcast_column
            Rectangle {
                height: 64
                border.width: 1
                border.color: "green"
                Layout.fillWidth: true;
                Layout.margins: 5
            RowLayout {
                id: bcHeaderRow
                height: parent.height
                property string selectedDate: qsTr("请选择日期")
                CComboBox {
                    Layout.preferredHeight: 30
                    Layout.preferredWidth: 200
                    Layout.leftMargin: 10
                    id: bctypeComBox
                    Component.onCompleted: {
                        var bcmodel = Services.getBroadcastTypes()
                        bcmodel.splice(0, 0, qsTr("请选择广播类型"))
                        bctypeComBox.model = bcmodel;
                    }


                }
                Item {
                    Layout.preferredWidth: 300

                }
                CCalendarBtn {
                    id: bcDateSlected
                    def: qsTr("选择广播日期")
                    width: 144
                    height: 32

                }

                Button {
                    implicitWidth: 64
                    implicitHeight: 32
                    contentItem:
                        Text {
                        text:  qsTr("查询")
                        font.pixelSize: 16
                        verticalAlignment:  Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter


                    }
                    background: Rectangle {
                        color: "green"
                    }
                    onClicked: {
                        var bc_type = bctypeComBox.currentText === qsTr("请选择广播类型")
                                ?"%": bctypeComBox.currentText;
                        var selectedDate = bcDateSlected.text===qsTr("选择广播日期")
                                ?'%': bcDateSlected.text
                        broadcastlog.model = Services.getBroadcastlogModel(
                                    bc_type,
                                    selectedDate);
                    }
                }
            }
}
            TableView {
                id: broadcastlog;
                Layout.fillWidth: true;
                Layout.fillHeight: true;
                Layout.margins: 5
                rowDelegate: row_delegate
                headerDelegate: headerDlg
                TableViewColumn {
                    role: "bc_type"
                    title: qsTr("广播类型");
                    delegate: cellDelegate
                }
                TableViewColumn {
                    role: "bc_content"
                    title: qsTr("广播内容");
                     delegate: cellDelegate
                }
                TableViewColumn {
                    role: "bc_receivers"
                    title: qsTr("接收方");
                    delegate: Row {
                        spacing: 20
                        Text {
                            text: styleData.value.split(/\n/)[0]
                            width: 100
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        Text {
                            text: "更多"
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    receiverPopup.open()
                                }
                            }
                            color: "green"
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        Popup {
                            id: receiverPopup
                            width: 300
                            height: 300
                            padding: 0
                            margins: 0
                            ColumnLayout {
                                anchors.fill: parent
                                Rectangle {
                                    color: "green"
                                    Layout.fillWidth: true
                                    height: 30
                                    RowLayout {
                                        width: parent.width
                                        height: 20
                                        id: rl
                                        Text {
                                            width: 60
                                            height: 30
                                            text: qsTr("接收方")
                                            color: "white"
                                        }
                                        Text {
                                            text: "X"
                                            width: 60
                                            height: 30
                                            font.pixelSize: 20
                                            color: "white"
                                            MouseArea {
                                                anchors.fill: parent
                                                onClicked: {
                                                    receiverPopup.close()
                                                }
                                            }
                                            //anchors.right: rl.right
                                            Layout.alignment: Qt.AlignRight

                                        }
                                    }


                                }
                                ListView {

                                    Layout.fillHeight: true;

                                    Layout.fillWidth: true;
                                    model: styleData.value.split(/\n/)
                                    delegate: Text {
                                        height: 30
                                        width: 300
                                        text: modelData
                                    }

                                }


                            }


                        }
                    }


                }

                TableViewColumn {
                    role: "bc_time"
                    title: qsTr("广播时间");
                    delegate: timeCellDelegate
                    width: 300;
                }


            }
            Component.onCompleted: {
                broadcastlog.model = Services.getBroadcastlogModel("%", "%");

            }

        }

        ColumnLayout{
            id: warn_column
            Rectangle {
                height: 64
                border.width: 1
                border.color: "green"
                Layout.fillWidth: true;
                Layout.margins: 5
            RowLayout {
                id: warnHeaderRow
                height: parent.height
                CComboBox {
                    Layout.preferredHeight: 30
                    Layout.preferredWidth: 200          
                    Layout.leftMargin: 10
                    id: warnLibComBox
                    Component.onCompleted: {
                        var mlist = Services.getLibRoomNames()
                        mlist.splice(0, 0, qsTr("选择库室名称"))
                        warnLibComBox.model = mlist;
                    }


                }
                Item {
                    Layout.preferredWidth: 300

                }
                CCalendarBtn {
                    id: warnDateSlected

                    def: qsTr("选择报警日期")
                    width: 144
                    height: 32

                }


                Button {
                    implicitWidth: 64
                    implicitHeight: 32
                    contentItem:
                        Text {
                        text:  qsTr("查询")
                        font.pixelSize: 16
                        verticalAlignment:  Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter


                    }
                    background: Rectangle {
                        color: "green"
                    }
                    onClicked: {
                        var libname = warnLibComBox.currentText === qsTr("选择库室名称")
                                ?"%": warnLibComBox.currentText;
                        var selectedDate = warnDateSlected.text===qsTr("选择报警日期")
                                ?'%': warnDateSlected.text
                        warnlog.model = Services.getWarnlogModel(
                                    libname, '%',
                                    selectedDate);
                    }
                }
            }
}
            TableView {
                id: warnlog;
                Layout.fillWidth: true;
                Layout.fillHeight: true;
                rowDelegate: row_delegate
                headerDelegate: headerDlg
                TableViewColumn {
                    role: "libname"
                    title: qsTr("库室名称");
                     delegate: cellDelegate
                }
                TableViewColumn {
                    role: "warn_level"
                    title: qsTr("警报级别");
                }
                TableViewColumn {
                    role: "warn_type"
                    title: qsTr("警报类别");
                     delegate: cellDelegate


                }
                TableViewColumn {
                    role: "warn_time"
                    title: qsTr("警报时间");
                    delegate: timeCellDelegate
                    width: 300;
                }


            }
            Component.onCompleted: {
                warnlog.model = Services.getWarnlogModel("%", "%", "%");

            }

        }



    }

    Rectangle {

        id : rootitem
        width: 100;
        height:parent.height;
        color: "white"
        anchors.left: parent.left
        ColumnLayout{
            id : navlyout
            width: parent.width
            height: parent.height
            spacing: 0
        }
        function addItem(text,index) {
            var component = Qt.createComponent("BaseNavBtn.qml");
            if (component.status === Component.Ready)
            {
                var itemrect = component.createObject(navlyout);
                itemrect.text = text;
                itemrect.index = index;
                itemrect.mclicked.connect(mmclicked); //信号的关联槽函数
            }

        }
        Component.onCompleted: {
            addItem(qsTr("授权日志"), 0)
            addItem(qsTr("通话日志"), 1)
            addItem(qsTr("广播日志"), 2)
            addItem(qsTr("警报日志"), 3)
        }

        //事件处理
        function mmclicked(index){
            stackLayout.currentIndex = index;
            /**/
            for (var i in navlyout.children)
            {
                //console.log("i: " + i)
                if (navlyout.children[i].index !== index)
                    navlyout.children[i].clearColor();
            }
            /**/
        }
    }

}
